package com.jinlia.show.modules.base.wxapp.entity;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.jinlia.show.common.core.entity.BaseEntity;
import com.jinlia.show.modules.base.config.DynamicSchemaConfig;
import com.jinlia.show.modules.base.constant.EnableStatusEnum;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 购物车
 */
@Data
@TableName(value = "wxapp_shopping_cart", schema = DynamicSchemaConfig.schema_wxapp, autoResultMap = true)
public class ShoppingCart extends BaseEntity {
    @TableId()
    private Long id;

    /**
     * 顾客id
     */
    private Long userId;
    /**
     * 商品id
     */
    private Long goodsId;
    private String goodsName;
    /**
     * 商品数量
     */
    private int num;
    /**
     * 单价
     */
    private BigDecimal unitPrice;
    /**
     * 原价格 = 单价*数量
     */
    private BigDecimal originalMoney;

    private String skuName;
    /**
     * 商品库存量单位
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private JSONObject skuItem;

    /**
     * 商品启用状态
     */
    private Integer enableStatus = EnableStatusEnum.ENABLE.getValue();

    public BigDecimal getOriginalMoney() {

        if (Objects.isNull(originalMoney) && Objects.nonNull(unitPrice)) {
            return unitPrice.multiply(BigDecimal.valueOf(num));
        }
        return originalMoney;
    }
}
